package com.youbu.demo;

import scala.Serializable;

import java.util.Comparator;


/**
 * 本地设置例子
 * @author sunfangwei
 **/
public class IntegralCountDemoTwo {

	public static void main(String[] args) {
		/*String filePath = "d:\\jf3.txt";
		String saveFilePath = "C:\\Users\\Administrator\\Desktop\\result";
		SparkConf conf = new SparkConf().setMaster("local").setAppName("ddd");
		conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
		JavaSparkContext sc = new JavaSparkContext(conf);
		JavaRDD<String> input = sc.textFile(filePath);
		//数据格式转换成K,V，K为用户名，V为值
		JavaPairRDD<String, Integer> count2 = input.filter(line -> !line.trim().equals("")).mapPartitionsToPair(line2 -> {
			List<Tuple2<String, Integer>> list2 = new ArrayList<Tuple2<String, Integer>>();
			Tuple2<String, Integer> tx = null;
			while (line2.hasNext()) {
				String[] valx = line2.next().split(",");
				tx = new Tuple2<>(valx[0], Integer.parseInt(valx[1]));
				list2.add(tx);
			}
			return list2.iterator();
		}).persist(StorageLevel.MEMORY_AND_DISK_SER());
		//将相用户的积分进行累加
		JavaPairRDD<String, Integer> count3 = count2.reduceByKey((a, b) -> a + b).persist(StorageLevel.MEMORY_AND_DISK_SER());
		//将K，V对调
		JavaPairRDD<Integer, String> count4	= count3.mapPartitionsToPair(linex->{
			List<Tuple2<Integer, String>> list2 = new ArrayList<Tuple2<Integer, String>>();
			while (linex.hasNext()) {
				Tuple2<String,Integer> tx= linex.next();
				list2.add(new Tuple2<Integer, String>(tx._2,tx._1));
			}
			return list2.iterator();
		}).sortByKey();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		count4.coalesce(100).saveAsTextFile(saveFilePath + "/" + sdf.format(new Date()));
		sc.close();*/

	}

	//自定义排序方法
	private static class Comp implements Comparator<String>,Serializable{
		@Override
		public int compare(String o1, String o2) {
			String []o1s = o1.split(" ");
			String []o2s = o2.split(" ");
			if(o1s[0].compareTo(o2s[0]) == 0){
				return o1s[1].compareTo(o2s[1]);
			} else {
				return -o1s[0].compareTo(o2s[0]);
			}

		}
	}


}

